from infoUi import wx
from otaMainUi import otaUi

class myOtaExecl(otaUi):
    def __init__(self, parent=None, title="MyTest",size = (900,700)):
        wx.Frame.__init__(self,parent=parent, title=title,size=size)
        self.panel = wx.Panel(self,size = (900,700))
        #初始化主界面
        self.setUpMainUi()
        #初始化状态机
        self.setUpMyStateMac()

    def otaDateDeal(self,buf):
        data = buf['data']
        buf['data'] = ''
        meth = self.dealWay.get(self.state)
        if(meth != None):
            if(meth(data)):
                pass
            else:
                report = "%s %s failed!\r\n" %(self.otaConfig['curStatus'],self.state)
                self.errorDebug(report)
                self.otaConfig['curStatus'] = ''
                self.otaRest()
                self.closeSerial()
        else:
            self.errorDebug("unknown state %s" %(self.state))
            self.otaConfig['curStatus'] = ''
            self.otaRest()
            self.closeSerial()

    def comDateRcv(self):
        buf={'data': ''}
        while(self.ser.isOpen()):
            try:
                self.recvDataLine(buf)
            except Exception as err:
                self.errorDebug("com thread:%s" %(err))
                self.closeSerial()

            self.debugToUi(buf['data'])
            if(buf['data'] != '' and self.state != "otaIdle"):
                #数据处理
                self.otaDateDeal(buf)
                pass

        if(self.ser.isOpen()):
            pass
        else:
            self.changeComStatus()
            self.otaRest()
            self.otaConfig['curStatus'] = ''
            print("COM disconnect !\r\n")


    #重写状态切换事件处理函数
    def onCheckSim(self):
        self.statusGaugeHide()
        self.timerStart()
        #发送SIM卡查询命令
        cmd = "AT+CPIN?\r\n"
        self.sendATCmd(cmd)
        self.statusTextSet(2,self.state)

    def onCheckNet(self):
        self.timerStart()
        #发送AT+CPSI?查询网络状态
        cmd = "AT+CPSI?\r\n"
        self.sendATCmd(cmd)
        self.statusTextSet(2,self.state)

    def onCmdSend(self):
        #启动状态定时器
        self.timerStart()
        print("on cmd send")
        #判断net模式
        if('ftp' in self.otaConfig['netMode'] or 'ftp' in self.otaConfig['curStatus']):
            netType = 0
            rPath = self.netConfig[netType]['remotePath']
            usr = self.netConfig[netType]['user']
            psw = self.netConfig[netType]['password']
        else:
            netType = 1
            rPath = self.otaCmdConf['httpRPath']
            usr = '1'
            psw = '1'

        #获取net对应配置
        host = self.netConfig[netType]['host']

        #调整路径格式
        if(rPath[-1] != '/'):
            rPath = rPath+'/'

        #判断升级方向
        if('up' in self.otaConfig['testMode'] or 'up' in self.otaConfig['curStatus']):
            pkgName = self.adiffConfig['upFile']
        else:
            pkgName = self.adiffConfig['downFile']

        #填充命令
        cmd = ("AT+CFOTA=0,%d,\"%s%s%s\",%s,%s\r\n" \
                %(netType, host, rPath, pkgName, usr, psw))
        print("send cmd:"+cmd)
        self.sendATCmd(cmd)
        self.statusTextSet(2,self.state)

if __name__ == '__main__':
    app = wx.App()
    model = myOtaExecl(title="While there is life,there is hope.")
    model.Centre()
    model.Show()
    app.MainLoop()
